import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router'
// import useUserStore from '@/stores/user'

const About = {
  render() {
    return h('div', 'About')
  },
}

const routes: Array<RouteRecordRaw> = [
  // { path: '/', component: Home },
  { path: '/', component: () => import('@/views/FeaturesMain.vue') },
  // {
  //   path: '/awsLogin',
  //   name: 'Login',
  //   component: () => import('@/views/AwsLogin.vue'),
  // },
  { path: '/about', component: About },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// router.addRoute(BaseRoute)

/* 加载模块路由 */
const modules = import.meta.glob('../views/**/route.ts')
Object.values(modules).forEach(async (value) => {
  const $route: any = await Promise.resolve(value())
  router.addRoute($route.default)
})

/* 解决异步路由问题 测试 */
let isFirst = true
router.beforeResolve((to) => {
  if (!to.matched.length && isFirst) {
    isFirst = false
    /* 如果没有匹配到路由，则再加载一次 */
    setTimeout(() => router.replace(to.fullPath), 300)
  }
})

// router.beforeEach(async (to, from, next) => {
//   const userStore = useUserStore()
//   if (to.name !== 'Login' && !userStore.token) {
//     /* 未登录 */
//     await next({ name: 'Login' })
//   } else if (userStore.token && to.name === 'Login') {
//     /* 已经登录 */
//     await next('/')
//   } else {
//     /* 其他 */
//     await next()
//   }
// })

// router.afterEach(() => {})

export default router
